package com.renbang.action.devoloper;

import java.io.UnsupportedEncodingException;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.renbang.model.common.AjaxResult;
import com.renbang.model.common.Page;
import com.renbang.model.sys.SysUser;
import com.renbang.service.sys.SysUserServiceImp;
import com.renbang.utils.EncrypUtil;
@Controller
@RequestMapping(value = "developer/user")
public class SysUserAction {
	
	@Autowired
	SysUserServiceImp sysUserServiceImp;
	@Autowired
	SqlSessionTemplate sqlSessionTemplate;
	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request,SysUser sysUser,Page page) {
		System.out.println(page.getCurrpage());
		Integer count = sysUserServiceImp.findTotal(sysUser);
		page.setCount(count);//查询总条数
		/*Double sum = page.getCount()+0.0;
		//点超标了返回第一页
		if(Math.ceil(sum/page.getSize())<page.getCurrpage()){
			page.setCurrpage(1);
		}*/
		return new AjaxResult(AjaxResult.STATUS_OK, page.getCurrpage()+"",null ,sysUserServiceImp.findInPage(sysUser, page), count);
	}
	
	//用户增加和修改
	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(SysUser sysUser) {
		if (sysUser.getUserId()!=null) {
			if(sysUser.getUserPass()!=null){
				try {
					sysUser.setUserPass(EncrypUtil.getMD5(sysUser.getUserPass()));//加密密码
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			sysUserServiceImp.modify(sysUser);
			return new AjaxResult(AjaxResult.STATUS_OK, null, "修改成功");
		} else {
			try {
				sysUser.setUserPass(EncrypUtil.getMD5(sysUser.getUserPass()));//加密密码
				sysUser.setRegTime(new Date());
				sysUser.setUserType("user");
				sysUser.setUserStatus("activate");
				
			} catch (UnsupportedEncodingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			sysUserServiceImp.add(sysUser);
			 return new AjaxResult(AjaxResult.STATUS_OK, null, "增加成功");
		}
	}
	
	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(Integer id) {
		Integer num = sysUserServiceImp.removeById(id);
		return new AjaxResult(AjaxResult.STATUS_OK, null, "成功删除"+num+"条");
	}
	
	@RequestMapping(value = "/editForm")
	public String editForm(Integer id, HttpServletRequest request) {
		SysUser sysUser;
		if (id != null) {
			sysUser = sysUserServiceImp.findById(id);
			request.setAttribute("obj", sysUser);
		}
		return "developer/user-form";
	}
	@RequestMapping(value = "/all")
	public @ResponseBody AjaxResult all() {
		SysUser sysUser = new SysUser();
		sysUser.setUserType("user");
		Integer count = sysUserServiceImp.findTotal(sysUser);
		return new AjaxResult(AjaxResult.STATUS_OK, null ,sysUserServiceImp.findByModel(sysUser), count);
	}
}
